package com.hnhuas.salary.modules.admin.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hnhuas.salary.common.BaseController;
import com.hnhuas.salary.common.utils.PageUtils;
import com.hnhuas.salary.common.utils.Query;
import com.hnhuas.salary.common.utils.R;
import com.hnhuas.salary.common.validator.ValidatorUtils;
import com.hnhuas.salary.modules.admin.entity.SalaryNoticeEntity;
import com.hnhuas.salary.modules.admin.entity.SalaryNoticeUserEntity;
import com.hnhuas.salary.modules.admin.entity.SalaryUserEntity;
import com.hnhuas.salary.modules.admin.service.SalaryNoticeService;
import com.hnhuas.salary.modules.admin.service.SalaryNoticeUserService;
import com.hnhuas.salary.modules.admin.service.SalaryUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wf.jwtp.annotation.RequiresRoles;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author weirdor
 * @email 179946422@qq.com
 * @date 2019-08-29 14:19:51
 */
@RestController
@RequestMapping("/admin/notice")
public class SalaryNoticeController  extends BaseController {
    @Autowired
    private SalaryNoticeService salaryNoticeService;

    @Autowired
    private SalaryNoticeUserService salaryNoticeUserService;

    @Autowired
    private SalaryUserService salaryUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){
        QueryWrapper<SalaryNoticeEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        PageUtils page = salaryNoticeService.queryPage(params,wrapper);
        List<SalaryNoticeEntity> aa = (List<SalaryNoticeEntity>) page.getList();
        for (int i = 0;i<aa.size();i++){
            QueryWrapper<SalaryNoticeUserEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", getLoginUserId(request));
            queryWrapper.eq("notice_id",aa.get(i).getId());
            List<SalaryNoticeUserEntity> userList = salaryNoticeUserService.list(queryWrapper);
            if (userList.size() != 0){
                aa.get(i).setIsTop(1);
            }else{
                aa.get(i).setIsTop(0);
            }
        }
        page.setList(aa);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        SalaryNoticeEntity salaryNotice = salaryNoticeService.getById(id);

        return R.ok().put("salaryNotice", salaryNotice);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresRoles("admin")
    public R save(@RequestBody SalaryNoticeEntity salaryNotice, HttpServletRequest request){
        salaryNotice.setCreateTime(System.currentTimeMillis());
        salaryNotice.setUpdateTime(System.currentTimeMillis());
        salaryNotice.setCreateId(getLoginUserId(request));
        salaryNotice.setStatus(0);
        salaryNoticeService.save(salaryNotice);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @RequiresRoles("admin")
    public R update(@RequestBody SalaryNoticeEntity salaryNotice){
        ValidatorUtils.validateEntity(salaryNotice);
        salaryNotice.setUpdateTime(System.currentTimeMillis());
        salaryNoticeService.updateById(salaryNotice);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{id}")
    @RequiresRoles("admin")
    public R delete(@PathVariable("id") Long id){
        salaryNoticeService.removeById(id);

        return R.ok();
    }


    /**
     * 查询所有签到
     */
    @GetMapping("/qian")
    @RequiresRoles("admin")
    public  R getAllQian(@RequestParam Map<String, Object> params){
        Long id= Convert.toLong(params.get("id"));
        if (id==null){
            return R.error("id错误");
        }
        PageUtils page =  salaryUserService.queryPage(params);

        List<SalaryUserEntity> list= (List<SalaryUserEntity>) page.getList();

        list.forEach(k->{
                QueryWrapper<SalaryNoticeUserEntity> queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("notice_id",id);
                queryWrapper.eq("user_id",k.getId());
                int count= salaryNoticeUserService.count(queryWrapper);
                if (count>0){
                    k.setIsQian(true);
                }else {
                    k.setIsQian(false);
                }

        });

        return R.ok().put("data",page);
    }

}
